Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

The Queue of Doubts

TIME LIMIT = 1 SEC.

  • Everybody loves Praveen sir's classes. He explains stuff so well, one can't help but get engaged in the classes and listen to what he has to say. But every once in a while, there comes a difficult topic that he has to explain, and students get doubts. Praveen sir being the dedicated teacher that he is, wants to clear everyone's doubts.
    He knows the total number of students who have doubts - N, and the number of doubts that each student has - Ni for the ith student. Praveen sir decides to form a queue, and clear 1 doubt of a student at a time. If the student has more doubts, then he goes back to the end of the queue and waits for his turn again. This applies to all students. Praveen sir takes 1 minute to clear 1 doubt. If a student has 0 doubts remaining, he goes back to his bench in the classroom.
    For a given value T, find out the number of doubts remaining of the student in front of the queue at the Tth minute.
Input Output
The first line consists of two space-separated integers - N, T.
N lines follow, each line containing Ni - the number of doubts the ith student.
A single line containing the number of doubts that the student in the front of the queue has at the Tth minute.
Constraints
  • 1 ≤ N,T ≤ 1000
  • 1 ≤ Ni ≤ 1000
Example Test Case
Input             Output
5 2
3
2
3
1
3
3
Solution

#include <iostream> #include <queue> using namespace std; int main() { int n, t, temp, i; cin >> n >> t; queue < int > doubt; for (i = 0; i < n; i++) { cin >> temp; doubt.push(temp); // push all doubts into queue. } for (i = 0; i < t; i++) { //until we reach the target time do the following. if (doubt.front() > 1) { doubt.front() -= 1; //decrement number of doubts. doubt.push(doubt.front()); // remove student from front and put them in the back. doubt.pop(); } else doubt.pop(); //if doubts are 0, then remove student from queue. } cout << doubt.front(); // print number of doubts in the front of // the queue after exiting the loop(which happens after we reach // the target time.) return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();